package com.anolesoft.yr.mbus.code.mapper;

import com.anolesoft.yr.mbus.code.entity.CodeItemRelDO;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.LocalDateTimeTypeHandler;

import java.util.List;

/**
 * @author zhj
 * @date 2020/9/22 16:11
 */
@Mapper
public interface CodeItemRelMapper {
    @Insert("insert into code_item_rel (" +
            "objuid, " +
            "fac_code, " +
            "code_main_uid, " +
            "code_item_uid, " +
            "ordernum," +
            "c_user_uid, " +
            "c_time, " +
            "m_user_uid, " +
            "m_time, " +
            "del_flag, " +
            "remark  " +
            ") values (" +
            "#{objuid,jdbcType=VARCHAR}, " +
            "#{factoryCode,jdbcType=VARCHAR}, " +
            "#{codeMainUid,jdbcType=VARCHAR}, " +
            "#{codeItemUid,jdbcType=VARCHAR}, " +
            "#{orderNum,jdbcType=INTEGER}, " +
            "#{createUserUid,jdbcType=VARCHAR}, " +
            "now(), " +
            "#{modifyUserUid,jdbcType=VARCHAR}, " +
            "now(), " +
            "0," +
            "#{remark,jdbcType=VARCHAR} " +
            ")")
    int insert(CodeItemRelDO codeItemRelDO);

    @Update("update code_item_rel set " +
            "fac_code = #{factoryCode,jdbcType=VARCHAR}," +
            "code_main_uid = #{codeMainUid,jdbcType=VARCHAR}, " +
            "code_item_uid = #{codeItemUid,jdbcType=VARCHAR}, " +
            "ordernum = #{orderNum,jdbcType=INTEGER}, " +
            "m_user_uid = #{modifyUserUid,jdbcType=VARCHAR}, " +
            "m_time = now(), " +
            "remark = #{remark,jdbcType=VARCHAR} " +
            " where " +
            "objuid = #{objuid,jdbcType=VARCHAR}")
    int update(CodeItemRelDO codeItemRelDO);

    @Update("update code_item_rel set del_flag = 1 where objuid = #{id,jdbcType=VARCHAR}")
    int delete(@Param("id") String id);

    @Results(id = "CodeItemRelResultMap", value = {
            @Result(id = true, column = "objuid", property = "objuid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "fac_code", property = "factoryCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "code_main_uid", property = "codeMainUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "code_item_uid", property = "codeItemUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ordernum", property = "orderNum", jdbcType = JdbcType.INTEGER),

            @Result(column = "c_user_uid", property = "createUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "c_time", property = "createTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "m_user_uid", property = "modifyUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "m_time", property = "modifyTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "del_flag", property = "deleteFlag", jdbcType = JdbcType.BOOLEAN),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),

            @Result(column = "code_item_uid", property = "codeItem", one = @One(select = "com.anolesoft.yr.mbus.code.mapper.CodeItemMapper.selectOneById", fetchType = FetchType.LAZY))
    })
    @Select("select " +
            "objuid, fac_code, code_main_uid, code_item_uid, ordernum, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark  " +
            "from code_item_rel where del_flag = 0 ")
    List<CodeItemRelDO> selectAll();

    @ResultMap("CodeItemRelResultMap")
    @Select("select " +
            "objuid, fac_code, code_main_uid, code_item_uid, ordernum, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark  " +
            "from code_item_rel where del_flag = 0 " +
            "and objuid = #{id,jdbcType=VARCHAR}")
    CodeItemRelDO selectOneById(@Param("id") String id);

    @ResultMap("CodeItemRelResultMap")
    @Select("select " +
            "objuid, fac_code, code_main_uid, code_item_uid, ordernum, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark  " +
            "from code_item_rel where del_flag = 0 " +
            "and code_main_uid = #{codeMainUid,jdbcType=VARCHAR}" +
            " order by ordernum")
    List<CodeItemRelDO> selectListByCodeMainUid(@Param("codeMainUid") String codeMainUid);

    @ResultMap("CodeItemRelResultMap")
    @Select("<script>" +
            "select " +
            "objuid, fac_code, code_main_uid, code_item_uid, ordernum, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark  " +
            "from code_item_rel where del_flag = 0 " +
            "<if test =\"o.factoryCode !=null and o.factoryCode != '' \">" +
            "   and fac_code = #{o.factoryCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.codeMainUid !=null and o.codeMainUid != '' \">" +
            "   and code_main_uid = #{o.codeMainUid, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.codeItemUid !=null and o.codeItemUid != '' \">" +
            "   and code_item_uid = #{o.codeItemUid, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.objuid !=null and o.objuid != '' \">" +
            "   and objuid = #{o.objuid, jdbcType=VARCHAR} " +
            "</if>" +
            " order by ordernum " +
            "</script>")
    List<CodeItemRelDO> selectList(@Param("o") CodeItemRelDO codeItemRelDO);

}
